import { loginApi, logout, getInfoApi ,getUserDetailById } from '@/api/user'
import { getToken, setToken, removeToken } from '@/utils/auth'
import { resetRouter } from '@/router'

const getDefaultState = () => {
  return {
    token: getToken(),
    name: '',
    avatar: '',
    userInfo:{}
  }
}

const state = getDefaultState()

const mutations = {
  RESET_STATE: (state) => {
    Object.assign(state, getDefaultState())
  },
  SET_TOKEN: (state, token) => {
    console.log(token,89);
    state.token = token
    // 将token做本地存储 存到cookie中
    setToken(token)
  },
  SET_NAME: (state, name) => {
    state.name = name
  },
  SET_AVATAR: (state, avatar) => {
    state.avatar = avatar
  },
  SET_INFO: (state, userInfo) => {
    state.userInfo = userInfo
  }
}

const actions = {
  // user login
  login(content, userInfo) {
    // const { username, password } = userInfo
    return new Promise((resolve, reject) => {
      loginApi(userInfo)
        .then(response => {
          console.log(response,11112);
          // 成功之后 
          // 获取token
          // 将token存储到vuex中
          content.commit('SET_TOKEN', response.data)
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },

  // get user info
  getInfo({ commit }) {
    return new Promise(async(resolve, reject) => {
      try {
        /* 获取用户信息 */
        const { data } = await getInfoApi()
        /* 根据用户id获取用户图像信息 */
        const res = await getUserDetailById(data.userId)
        console.log(res, 555)
        commit('SET_NAME', data.username)
        commit('SET_INFO', data)
        console.log(data,'4========');
        /* 存储图片 */
        commit('SET_AVATAR', res.data.staffPhoto)
        resolve(data)
      } catch (error) {
        reject(error)
      }
      // getInfoApi().then(response => {
      //   console.log(response,777);
      //   const { data } = response
        /* 
          1-permisssion.js===触发getInfo函数帮助获取用户信息
          2-store/module/user.js===发送请求获取用户信息
          3-将用户信息存储到state中
            3.1 state中顶一个userInfo对象
            3.2 在mutations中定义一个函数来更改userInfo==SET_INFO数据的
            3.3 调用SET_INFO来更改数据
          4-将数据展示到页面上
           */
        // if (!data) {
        //   return reject('Verification failed, please Login again.')
        // }

        // const { name, avatar } = data

        // commit('SET_NAME', name)
        // commit('SET_AVATAR', avatar)
        // resolve(data)
      // }).catch(error => {
      //   reject(error)
      // })
    })
  },

  // user logout
  logout({ commit, state }) {
    return new Promise((resolve, reject) => {
      // logout(state.token).then(() => {
      //   removeToken() // must remove  token  first
      //   resetRouter()
      //   commit('RESET_STATE')
      //   resolve()
      // }).catch(error => {
      //   reject(error)
      // })
      removeToken()
      resetRouter()
      commit('RESET_STATE')
      resolve()
      .catch(error => {
        reject(error)
      })
    })
  },

  // remove token
  resetToken({ commit }) {
    return new Promise(resolve => {
      removeToken() // must remove  token  first
      commit('RESET_STATE')
      resolve()
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}

